﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using TWiStEr.Math;

namespace Phys.UI {
	public partial class PhysicsGLWindow {
		private static Dictionary<string, object> settings;
		private void LoadSettings(string filename) {
			settings = new Dictionary<string, object>() {
				{"ShowVelocity", false}
			};
			try {
				filename = Path.ChangeExtension(filename, ".set");
				XmlDocument doc = new XmlDocument();
				doc.Load(filename);
				foreach (XmlAttribute attr in doc.DocumentElement.Attributes) {
					switch (attr.Name) {
						case "ShowVelocity": settings["ShowVelocity"] = bool.Parse(attr.Value); break;
					}
				}
				foreach (XmlNode node in doc.DocumentElement) {
					switch (node.Name) {
						case "Camera":
							foreach (XmlAttribute attr in node.Attributes) {
								Vector pos = cam.Position;
								Vector view = cam.View;
								Vector up = cam.Up;
								switch (attr.Name) {
									case "Position":
										pos = Vector.Parse(attr.Value);
										break;
									case "Up":
										up = Vector.Parse(attr.Value);
										break;
									case "View":
										view = Vector.Parse(attr.Value);
										break;
								}
								cam.SetPosition(pos, view, up);
							}
							break;
						default:
							Console.Error.WriteLine("Setting not implemented: " + node.OuterXml);
							break;
					}
				}
			} catch (FileNotFoundException) {
				Console.Error.WriteLine("You can create a settings file for your world: " + filename);
			} catch (Exception ex) {
				Console.Error.WriteLine("Cannot load settings for world.");
				Console.Error.WriteLine(ex);
			}
		}
	}
}
